const express = require('express');
const note = express.Router();
const Note = require('../model/note')
//查询当前用户所有笔记
note.post('/index', (req, res) => {
  Note.find({ username: req.query.username }).then(result => res.send({
    code: 200, data: result
  }))
})
//查询某条笔记
note.get('/find', (req, res) => {
  Note.findOne({ _id: req.query.id }).then(result => res.send({
    code: 200, data: result
  }))
})
//更新某条笔记
note.post('/update', (req, res) => {
  Note.updateOne({ _id: req.body.id }, { title: req.body.title, content: req.body.content }).then(result => res.send({
    code: 200, data: result
  }))
})
//添加笔记
note.post('/add', (req, res) => {
  Note.create(req.body)
    .then(doc => console.log(doc))
    .catch(err => console.log(err))
  res.send('ok')
})
//删除笔记
note.post('/delete', (req, res) => {
  Note.findOneAndDelete({ _id: req.query.id })
    .then(result => res.send({
      code: 200
    }))
})
module.exports = note;